// News fetching YQL query
var szNewsFeed = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20rss%20where%20url%3D%22http%3A%2F%2Frss.news.yahoo.com%2Frss%2Ftopstories%22%20&format=json&diagnostics=true";

// number of feed items to show.
var nFeedItemsToShow = 5;

// get news feed and add it into given div id
function getNewsFeed(divid) {
	try {
		// adjust height
		gadgets.window.adjustHeight();
		// clean the news
		document.getElementById("news").innerHTML = "Loading News ...";
		// 1. generate URL based on user name
		var URL = szNewsFeed;
		// 2. read URL output
		readURL_ajax(URL, getNewsFromJSON, divid);

	} catch (ex) {
		handleException("getNewsFeed()" + ex.description);
	}
}

// this function is a call back function. 
// input : oJSON JSON formatted object as a result of the readURL_ajax() call.
// divid : id of the division that will receive this information.
function getNewsFromJSON(oJSON, divid) {
	try {
		showMessage("in getNewsFromJSON CallBack()");

		if (oJSON != null) {
			showMessage(oJSON.query.count);

			var szEntry = "";
			for ( var i = 0; i < oJSON.query.count && i < nFeedItemsToShow ; i++) {
				try {
					// for each result entry, add it into the division.
					szEntry = oJSON.query.results.item[i].description;
					document.getElementById(divid).innerHTML += szEntry;
				} catch (ex) {
					handleMessage("getTwitFromJSON() : loop: " + ex.description);
				}
			}
			// adjust height of the gadget.
			gadgets.window.adjustHeight(nFeedItemsToShow * 200);
		} else {
			showMessage("getNewsFromJSON(): empty news feed");
		}
	} catch (ex) {
		handleMessage("getNewsFromJSON() : " + ex.description);
	}
}